<?php
/*
 * 支付回调
 */
namespace Home\Controller;

use Think\Controller;

class NotifyUrlController extends Controller {

    //续费支付回调
    public function distPayCallback(){
        $post = $this->post_data();
        $post_data = $this->xml_to_array($post);

        $out_trade_no = isset($post_data['out_trade_no'])?$post_data['out_trade_no']:'';
        if($post_data['return_code']=='SUCCESS' && !empty($out_trade_no)){
            $oOrderModel     = new \Common\Model\OrderModel;

            //当前订单信息
            $order_find = $oOrderModel->where(['order_no'=>$out_trade_no])->find();
            if(!empty($order_find) && $order_find['order_status'] == $oOrderModel::ORDER_STATUS_PAY){
                $this->return_success();
            }

            //当前会员信息
            $user_find = M('User')->where(['user_id'=>$order_find['user_id']])->find();

            if(!empty($order_find) && !empty($user_find)){

                try {
                    //开启事务
                    M()->startTrans();
                    //更新订单状态
                    $order_save_status = $oOrderModel->end($order_find['order_id']);

                    //会员当前过期时间，如果是大于当前时间，没过期，原基础上增加
                    $expiration_time = $user_find['expiration_time'];
                    //如果小于当前时间，证明当前已过期，在当前时间戳的基础上增加
                    if($expiration_time<time()){
                        $expiration_time = time();
                    }

                    //增加用户到期时间
                    $user_data = [];
                    $user_data['user_id'] = $order_find['user_id'];
                    $user_data['expiration_time'] = strtotime('+'.$order_find['count'].' month',$expiration_time);//加几个月时间，$order_find['count']为客户购买数量
                    $user_save_status = M('User')->save($user_data);

                    if($order_save_status && $user_save_status){
                        $this->return_success();
                        //提交事务
                        M()->commit();
                    }
                } catch (\Exception $e) {
                    //事务回滚
                    M()->rollback();
                }
            }
        }

    }

    //扩容支付回调
    public function distPayAddCallback(){
        $post = $this->post_data();
        $post_data = $this->xml_to_array($post);

        $out_trade_no = isset($post_data['out_trade_no'])?$post_data['out_trade_no']:'';
        if($post_data['return_code']=='SUCCESS' && !empty($out_trade_no)){
            $oOrderModel     = new \Common\Model\OrderModel;

            //当前订单信息
            $order_find = $oOrderModel->where(['order_no'=>$out_trade_no])->find();
            if(!empty($order_find) && $order_find['order_status'] == $oOrderModel::ORDER_STATUS_PAY){
                $this->return_success();
            }

            //当前会员信息
            $user_find = M('User')->where(['user_id'=>$order_find['user_id']])->find();

            if(!empty($order_find) && !empty($user_find)){

                try {
                    //开启事务
                    M()->startTrans();
                    //更新订单状态
                    $order_save_status = $oOrderModel->end($order_find['order_id']);

                    //增加用户端口
                    $user_data = [];
                    $user_data['user_id'] = $order_find['user_id'];
                    $user_data['port'] = $user_find['port']+$order_find['port_count'];
                    $user_save_status = M('User')->save($user_data);

                    if($order_save_status && $user_save_status){
                        $this->return_success();
                        //提交事务
                        M()->commit();
                    }
                } catch (\Exception $e) {
                    //事务回滚
                    M()->rollback();
                }
            }
        }
    }

    /*
    * 给微信发送确认订单金额和签名正确，SUCCESS信息
    */
    private function return_success(){
        $return['return_code'] = 'SUCCESS';
        $return['return_msg'] = 'OK';
        $xml_post = '<xml>
                    <return_code>'.$return['return_code'].'</return_code>
                    <return_msg>'.$return['return_msg'].'</return_msg>
                    </xml>';
        echo $xml_post;exit;
    }


    /**
     * 将xml转为array
     * @param string $xml
     * return array
     */
    public function xml_to_array($xml){
        if(!$xml){
            return false;
        }
        //将XML转为array
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $data;
    }


    //接受POST数据XML个数
    function post_data(){
        $receipt = $_REQUEST;
        if($receipt==null){
            $receipt = file_get_contents("php://input");
            if($receipt == null){
                $receipt = $GLOBALS['HTTP_RAW_POST_DATA'];
            }
        }
        return $receipt;
    }

}